import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';

export interface IJwtPayLoad {
  id: number,
  platform: string,
}

@Injectable()
export class AuthService {
  constructor(
    private readonly jwtService: JwtService,
  ) {
  }

  public generateToken(payload: IJwtPayLoad): string {
    return this.jwtService.sign({ ...payload }, {
      expiresIn: '7d', // 设置7天过期时间
    });
  }

  public verifyToken(token: string): IJwtPayLoad {
    try {
      return this.jwtService.verify(token);
    } catch (e) {
      throw  new HttpException('当前token无效', HttpStatus.OK);
    }
  }
}
